package com.library.web;

import java.awt.image.RenderedImage;
import java.io.IOException;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

import com.library.utils.CodeUtil;
import com.library.entity.Student;
import com.library.service.StudentService;

import my.bean.Msg;
import my.db.Page;
import my.mvc.Annotation.JSON;
import my.mvc.RequestContext;

@SuppressWarnings("unchecked")
public class StudentAction {

	private StudentService service = new StudentService();
	
	public void login(RequestContext rc) {
		String uname=rc.param("uname");
		String passwd=rc.param("passwd");
		String code = rc.param("code");
		Student student= service.validStudent(uname, passwd);
		String vcode = rc.sessionAttr("code")+"";
		if(student == null) {
			rc.reqAttr("msg","账号或密码不正确");//request.setAttribute();
			rc.forward("/login.jsp");//服务器端跳转
		}
		else if(!code.equalsIgnoreCase(vcode)) {
			rc.reqAttr("msg","验证码错误！");
			rc.forward("/login.jsp");
		}
		else{
			rc.sessionAttr("student",student);
			//重定向跳转：避免表单重复刷新
			rc.redirect("/indexfirst.jsp");
		}
	}
	//验证码
	public void vcode(HttpServletRequest req, HttpServletResponse resp) {
		 // 调用工具类生成的验证码和验证码图片
       Map<String, Object> codeMap = CodeUtil.generateCodeAndPic();

       // 将四位数字的验证码保存到Session中。
       HttpSession session = req.getSession();
       session.setAttribute("code", codeMap.get("code").toString());

       // 禁止图像缓存。
       resp.setHeader("Pragma", "no-cache");
       resp.setHeader("Cache-Control", "no-cache");
       resp.setDateHeader("Expires", -1);

       resp.setContentType("image/jpeg");

       // 将图像输出到Servlet输出流中。
       ServletOutputStream sos;
       try {
           sos = resp.getOutputStream();
           ImageIO.write((RenderedImage) codeMap.get("codePic"), "jpeg", sos);
           sos.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
	}
	/**
	 * 列表
	 */
	@JSON
	public Page<Student> list(RequestContext rc) {
		// 获得分页信息, layui表格分页参数：page、limit
		Page<Student> page = rc.form(Page.class);
		String name = rc.param("name");
		service.list(page, name);
		return page;
	}

	/**
	 * 保存
	 */
	@JSON
	public Msg save(RequestContext rc) throws Exception {
		Student a = rc.form(Student.class);
		if (a.getId() == 0) {
			service.save(a);
		} else {
			service.update(a);
		}

		return new Msg(true);
	}

	/**
	 * 获得
	 */
	public void get(RequestContext rc) {
		long id = rc.param("id", 0L);
		Student student = service.get(id);
		rc.reqAttr("student", student);
		rc.forward("/page/student/student_form.jsp");
	}
	
	/**
	 * 删除
	 * */
	@JSON
	public Msg multiDel(RequestContext rc) {
		String ids = rc.param("ids");
		if(StringUtils.isNotBlank(ids)) {
			String[] arr = ids.split(",");
			for(String id : arr) {
				service.delete(Long.parseLong(id));
			}
		}
		return new Msg();
	}
}
